package cn.com.infcn.monitor.task.jvm.impl;

import java.io.IOException;
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import javax.management.MBeanServerConnection;
import javax.management.ObjectName;

import org.apache.mina.core.session.IoSession;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.com.infcn.monitor.bean.IFCResponseBean;
import cn.com.infcn.monitor.jvm.bean.IFCJVMGarbageCollector;
import cn.com.infcn.monitor.task.impl.BaseTask;
import cn.com.infcn.monitor.util.IFCBeanUtils;
import cn.com.infcn.monitor.util.IFCMinitorConstanst;

/**
 * Java 虚拟机中的垃圾回收器。
 * 
 * @author jjs
 *
 */
public class JVMGarbageCollectorTask extends BaseTask {

	private final static Logger logger = LoggerFactory.getLogger(JVMGarbageCollectorTask.class);

	public JVMGarbageCollectorTask(IoSession session, Object message) {
		super(session, message);
	}

	@Override
	public void execute() throws Exception {
		IFCResponseBean result = jvmGarbageCollector();
		sendResponse(result);
	}

	public IFCResponseBean jvmGarbageCollector() {
		IFCResponseBean result = new IFCResponseBean();
		List<IFCJVMGarbageCollector> jvmGarbageCollectors = null;
		IFCJVMGarbageCollector jvmGarbageCollector = null;

		MBeanServerConnection mbs = null;
		try {
			// 获取MBeanServerConnection
			mbs = MBeanServerConnectionFactory.getInstance(IFCMinitorConstanst.JMXURL);
		} catch (IOException e) {
			logger.error("jmx.url 连接异常！" + e.getMessage());
			result.setResponseCode(IFCMinitorConstanst.MONITOR_RESPONSE_CODE_ERROR);
			result.setResponseMsg("jmx.url 连接异常！");
			return result;
		}

		try {

			ObjectName gcName = new ObjectName(ManagementFactory.GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",*");

			Set<ObjectName> mbeans = mbs.queryNames(gcName, null);
			if (mbeans != null) {
				jvmGarbageCollectors = new ArrayList<IFCJVMGarbageCollector>();
				Iterator<ObjectName> iterator = mbeans.iterator();
				while (iterator.hasNext()) {

					jvmGarbageCollector = new IFCJVMGarbageCollector();

					ObjectName objName = (ObjectName) iterator.next();
					GarbageCollectorMXBean gc = ManagementFactory.newPlatformMXBeanProxy(mbs,
							objName.getCanonicalName(), GarbageCollectorMXBean.class);

					IFCBeanUtils.copyProperties(gc, jvmGarbageCollector);
					jvmGarbageCollectors.add(jvmGarbageCollector);

				}
			}
			result.setResult(jvmGarbageCollectors);
			result.setResponseCode(IFCMinitorConstanst.MONITOR_RESPONSE_CODE_OK);
		} catch (Exception e) {
			logger.error(e.getMessage());
			result.setResponseCode(IFCMinitorConstanst.MONITOR_RESPONSE_CODE_ERROR);
			result.setResponseMsg(e.getMessage());
		}

		return result;
	}

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		JVMGarbageCollectorTask task = new JVMGarbageCollectorTask(null, null);
		IFCResponseBean result = task.jvmGarbageCollector();
		List<IFCJVMGarbageCollector> ifcGarbageCollectors = (List<IFCJVMGarbageCollector>) result.getResult();

		for (IFCJVMGarbageCollector ifcGarbageCollector : ifcGarbageCollectors) {
			System.out.println(ifcGarbageCollector.getName());
			System.out.println(ifcGarbageCollector.getCollectionCount());
			System.out.println(ifcGarbageCollector.getCollectionTime());
		}
	}

}
